package com.idrive.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

import com.idrive.R;
import com.idrive.database.DatabaseUtility;
import com.idrive.database.dao.CommonDataDao;
import com.idrive.database.dto.CommonData;

public class SMSReaderActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sms_reader_settings);

		final CommonDataDao dao = new CommonDataDao(this);
		CommonData data = dao.getCommonData();

		String option = data.getReadSms();

		if (DatabaseUtility.SMS_READER_OPTION_INACTIVE.equals(option)) {
			RadioButton radioButton = (RadioButton) findViewById(R.id.inactiveRadioButton);
			radioButton.setChecked(Boolean.TRUE);
		} else if (DatabaseUtility.SMS_READER_OPTION_ALL_SMS.equals(option)) {
			RadioButton radioButton = (RadioButton) findViewById(R.id.allRadioButton);
			radioButton.setChecked(Boolean.TRUE);
		} else if (DatabaseUtility.SMS_READER_OPTION_PHONEBOOK.equals(option)) {
			RadioButton radioButton = (RadioButton) findViewById(R.id.phonebookRadioButton);
			radioButton.setChecked(Boolean.TRUE);
		}

		final Button button = (Button) findViewById(R.id.okSMSReaderButton);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				final RadioButton option1 = (RadioButton) findViewById(R.id.inactiveRadioButton);
				final RadioButton option2 = (RadioButton) findViewById(R.id.allRadioButton);
				final RadioButton option3 = (RadioButton) findViewById(R.id.phonebookRadioButton);

				if (option1.isChecked()) {
					CommonData data = new CommonData();
					data.setReadSms(DatabaseUtility.SMS_READER_OPTION_INACTIVE);
					dao.updateCommonData(data);
					Toast.makeText(SMSReaderActivity.this,
							"SMS Reader has been disabled.", Toast.LENGTH_LONG)
							.show();

				} else if (option2.isChecked()) {
					CommonData data = new CommonData();
					data.setReadSms(DatabaseUtility.SMS_READER_OPTION_ALL_SMS);
					dao.updateCommonData(data);
					Toast.makeText(
							SMSReaderActivity.this,
							"SMS Reader will be activated for all incoming SMS.",
							Toast.LENGTH_LONG).show();
				} else if (option3.isChecked()) {
					CommonData data = new CommonData();
					data.setReadSms(DatabaseUtility.SMS_READER_OPTION_PHONEBOOK);
					dao.updateCommonData(data);
					Toast.makeText(
							SMSReaderActivity.this,
							"SMS Reader will be activated only for phonebook contacts.",
							Toast.LENGTH_LONG).show();
				}

				Intent myIntent = new Intent(SMSReaderActivity.this,
						MainActivity.class);

				SMSReaderActivity.this.startActivity(myIntent);
			}
		});
	}
}
